<template>
  <div :class="className" :style="{height:height,width:width}"/>
</template>
<script>
require('echarts/theme/macarons')
export default {
  data(){
    return {
      chart: null
    }
  },
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    },
    options:{
      type:Object,
      default(){
        return {}
      }
    }
  },
  watch: {
    options: {
      handler(newValue) {
        this.chart.setOption(newValue)
      },
      immediate: false,
      deep: true
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = this.$echarts.init(this.$el,'macarons')
      this.chart.setOption(this.options, true)
    }
  }
}
</script>
<style>

</style>
